#ifndef CIRCLETHREAD_H
#define CIRCLETHREAD_H

#include <QObject>
#include <QThread>
#include <QPoint>
#include <QDebug>
#include <cmath>

// 工作对象：实际在工作线程中运行
class CircleWorker : public QObject
{
    Q_OBJECT

public:
    explicit CircleWorker(QObject *parent = nullptr);
    void requestStop() { m_shouldStop = true; }

public slots:
    void doWork(const QPoint &center, int radius);

signals:
    void pointGenerated(const QPoint &pt);
    void workFinished();

private:
    bool m_shouldStop;
};

// 主对象：始终在主线程，供QML使用
class CircleThread : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QPoint center READ center WRITE setCenter NOTIFY centerChanged)
    Q_PROPERTY(int radius READ radius WRITE setRadius NOTIFY radiusChanged)
    Q_PROPERTY(bool running READ running NOTIFY runningChanged)

public:
    explicit CircleThread(QObject *parent = nullptr);
    ~CircleThread();

    QPoint center() const { return m_center; }
    void setCenter(const QPoint &center) {
        if (m_center != center) {
            m_center = center;
            emit centerChanged();
        }
    }

    int radius() const { return m_radius; }
    void setRadius(int radius) {
        if (m_radius != radius) {
            m_radius = radius;
            emit radiusChanged();
        }
    }

    bool running() const { return m_running; }

public slots:
    void start();
    void stop();

signals:
    void circlePoint(const QPoint &pt);
    void centerChanged();
    void radiusChanged();
    void runningChanged();
    void finished();

private slots:
    void onWorkerFinished();
    void onPointGenerated(const QPoint &pt);
    void onThreadStarted();

private:
    QPoint m_center;
    int m_radius;
    QThread *m_thread;
    CircleWorker *m_worker;
    bool m_running;
    bool m_workStarted;  // 防止重复调用 doWork
};

#endif // CIRCLETHREAD_H

